﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ECode.ECommon.Extensions;
using ECode.EComPort.Base;
using ECode.EComPort.Common;

namespace ECode.ModbusTester.Lib.ViewModel
{
    public class SendViewModel : BaseViewModel
    {
        #region
        private string _sendData;
        /// <summary>
        /// 接收数据
        /// </summary>
        public string SendData { get { return _sendData; } set { _sendData = value; OnPropertyChanged("RecData"); } }
        /// <summary>
        /// 是否16进制显示
        /// </summary>
        public bool IsHexShow { get; set; }
        /// <summary>
        /// 是否新行
        /// </summary>
        public bool IsNewLine { get; set; }

        /// <summary>
        /// 是否自动发送
        /// </summary>
        public bool IsAuto { get; set; }

        /// <summary>
        /// 自动发送间隔
        /// </summary>
        public string Interval { get; set; }
        /// <summary>
        /// 串口对象
        /// </summary>
        public ESerialPort Com { get; set; }
        /// <summary>
        /// 编码方式
        /// </summary>
        public string ComDataType { get; set; }

        #endregion

        #region Field
        private AutoResetEvent _wait = new AutoResetEvent(false);
        #endregion

        #region

        public delegate void OnSendCompleteHandler(bool sendSuccess, int sendLen);

        public event OnSendCompleteHandler OnSendComplete;
        #endregion

        #region
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="data"></param>
        public void Send(string data)
        {
            SendData += data;
            if (IsHexShow)
                SendData = Encoding.UTF8.GetBytes(SendData).Aggregate(string.Empty, (a, b) => a += b.ToString("X2") + " ");
        }
        /// <summary>
        /// 清空数据
        /// </summary>
        public void Clear()
        {
            SendData = string.Empty;
        }
        /// <summary>
        /// 发送
        /// </summary>
        public void Send()
        {
            if (Com == null || !Com.ComObj.IsOpen || string.IsNullOrWhiteSpace(SendData))
                return;
            if (IsNewLine)
                SendData += "\r\n";
            byte[] tmp = null;
            var cidt = (ComInfoDataType)Enum.Parse(typeof(ComInfoDataType), ComDataType);
            switch (cidt)
            {
                case ComInfoDataType.Hex:
                    tmp = SendData.ToBytes("HEX");
                    break;
                case ComInfoDataType.ASCII:
                    tmp = Encoding.ASCII.GetBytes(SendData);
                    break;

                case ComInfoDataType.Gb2312:
                    tmp = Encoding.GetEncoding("gb2312").GetBytes(SendData);
                    break;
                case ComInfoDataType.Other:
                case ComInfoDataType.UTF8:

                    tmp = Encoding.UTF8.GetBytes(SendData);
                    break;
                default:
                    tmp = SendData.ToBytes("HEX");
                    break;
            }

            if (tmp == null)
            {
                OnSendComplete(false, 0);
                return;
            }
            var b = Com.Send(tmp);
            if (OnSendComplete != null)
                OnSendComplete(b, tmp.Length);
        }
        #endregion
    }
}
